package org.speed.spring.core.aop.cglb;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy<T> implements MethodInterceptor {
	
	private T target;
	
	public CglibProxy(T target) {
		this.target = target;
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		
		System.out.println("before");
		
		Object rs = method.invoke(target, args);
		
		System.out.println("after");

		return rs;
	}
	
	public T getProxy() {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(target.getClass());
		enhancer.setCallback(this);
		return (T) enhancer.create();
	}

}
